﻿#include <stdlib.h>
#include <QSlider>
#include <QFont>
#include <QPalette>
#include <QAction>
#include <QTimer>
#if QT_VERSION <= 0x050000
#ifdef __linux__
#include <QPlastiqueStyle>
#endif
#endif

#include "objectClass.h"
#include "mymainwindow.h"

MyMainWindow::MyMainWindow(char *fileName_, QWidget *parent) :
    QMainWindow(parent), centralView(NULL), typeSelected(NULL), statusBar(NULL)
{
    if(fileName_==NULL){
        exit(-1);
    }
    centralView = new ImageView(fileName_,this);

    initToolBar();
    initMenuBar();
    initStateBar();
    setCentralWidget(centralView);
    centralView->setFocus();
    connect(centralView,SIGNAL(changeWindowName(QString)),this,SLOT(changeImageName(QString)));
    connect(centralView,SIGNAL(finished_exit()),this,SLOT(close()));

    //Create a timer
    vis_timer_ = new QTimer(this);
    vis_timer_->start(34);//5ms

    connect(vis_timer_, SIGNAL(timeout()), this, SLOT(timeoutSlot()));
}

void MyMainWindow::initToolBar()
{
    typeSelected = new QToolBar(this);
    AnoObjClass_List* ocl = centralView->getObjClass();
    for(int i=0; i<ocl->size(); i++) {
//        printf("obj: %3d %16s %10s (%02x %02x %02x)\n",
//               ocl[i].id, ocl[i].name.c_str(), ocl[i].color.c_str(),
//               ocl[i].r, ocl[i].g, ocl[i].b);

        QAction* act = typeSelected->addAction(QString::fromStdString((*ocl)[i].name));
        act->setData(i);

        QString sKey = QString("%1").arg(i);
        act->setShortcut(QKeySequence::fromString(sKey));
    }

    QAction* act = typeSelected->addAction(QString("ShowAll"));
    act->setData(99);
    act->setShortcut(QKeySequence::fromString("A"));

    connect(typeSelected, SIGNAL(actionTriggered(QAction*)), centralView, SLOT(changeObjClass(QAction*)));
    connect(typeSelected, SIGNAL(actionTriggered(QAction*)), this, SLOT(changeTypeName(QAction*)));

    this->addToolBar(Qt::TopToolBarArea, typeSelected);
}

void MyMainWindow::initMenuBar()
{
    initAction();
    QMenu *file = menuBar()->addMenu(tr("&File"));
    file->addAction(openA);
    file->addAction(closeA);
    file->addAction(exitA);

    QMenu *edit = menuBar()->addMenu(tr("&Edit"));
    edit->addAction(copyA);
    edit->addAction(cutA);
    edit->addAction(pasteA);
}

void MyMainWindow::initStateBar()
{
    statusBar = new QStatusBar(this);
//    statusBar->showMessage(tr("(Northwestern Polytechnical University)"),2000);

    QFont ft = QFont("Times",12,QFont::Bold);
    QPalette pa;
    pa.setColor(QPalette::WindowText,Qt::red);
    // creat label
    labelTypeName = new QLabel(this);
    labelTypeName->setFrameStyle(QFrame::Panel);
    labelTypeName->setPalette(pa);
    labelTypeName->setFont(ft);
    labelTypeName->setText("Empty Type now");

    labelImageName = new QLabel(this);
    labelImageName->setFrameStyle(QFrame::Panel);
    labelImageName->setPalette(pa);
    labelImageName->setFont(ft);
    labelImageName->setText("Empty Image now");

     //    QProgressBar *my_progressbar = new QProgressBar(this);
    //    statusBar->addPermanentWidget(my_progressbar);
    //    my_progressbar->setTextVisible( true );
    //    my_progressbar->setValue(30);

    QLabel *ImageTips = new QLabel(this);
    ImageTips->setFont(ft);
    ImageTips->setText("<font color=green>Process: Image Index [</font>");
    ///
    QLabel *TypeTips = new QLabel(this);
    TypeTips->setText("<font color=green>]       Type: [</font>");
    ///
    QLabel *tipsEnd = new QLabel(this);
    tipsEnd->setFont(ft);
    tipsEnd->setText("<strong><font color=green>]</strong></font>");
    ///
    frameslider = new QSlider(Qt::Horizontal);
    frameslider->setMinimum(0);
    frameslider->setMaximum(centralView->getTotalLine());
    frameslider->setValue(0);

    QToolButton *backButton = new QToolButton();
    backButton->setObjectName(QStringLiteral("backButton"));
    backButton->setArrowType(Qt::LeftArrow);

    QToolButton *stopButton = new QToolButton();
    stopButton->setObjectName(QStringLiteral("stopButton"));

    QToolButton *playButton = new QToolButton();
    playButton->setObjectName(QStringLiteral("playButton"));

    QToolButton *nextButton = new QToolButton();
    nextButton->setObjectName(QStringLiteral("nextButton"));
    nextButton->setArrowType(Qt::RightArrow);

    stopButton->setText("Stop");
    playButton->setText("Play");

    connect(playButton, SIGNAL(clicked()), this, SLOT(playButtonPressed()));
    connect(stopButton, SIGNAL(clicked()), this, SLOT(stopButtonPressed()));
    connect(backButton, SIGNAL(clicked()), this, SLOT(backButtonPressed()));
    connect(nextButton, SIGNAL(clicked()), this, SLOT(nextButtonPressed()));

    statusBar->addPermanentWidget(backButton);
    statusBar->addPermanentWidget(stopButton);
    statusBar->addPermanentWidget(playButton);
    statusBar->addPermanentWidget(nextButton);
#if QT_VERSION <= 0x050000
#ifdef __linux__
    frameslider->setStyle(new QPlastiqueStyle());
#endif
#endif

    connect(centralView, SIGNAL(currentIndexChanged(int)), frameslider, SLOT(setValue(int)));
    connect(frameslider, SIGNAL(valueChanged(int)), centralView, SLOT(changeCurrentIndex(int)));
    statusBar->addPermanentWidget(frameslider);

    statusBar->addPermanentWidget(ImageTips);
    statusBar->addPermanentWidget(labelImageName);
    statusBar->addPermanentWidget(TypeTips);
    statusBar->addPermanentWidget(labelTypeName);
    statusBar->addPermanentWidget(tipsEnd);
    this->setStatusBar(statusBar);
}

void MyMainWindow::initAction()
{
     openA  = new QAction(tr("Open"), this);
     closeA = new QAction(tr("Close"), this);
     exitA  = new QAction(tr("Exit"), this);
     copyA  = new QAction(tr("Copy"), this);
     cutA   = new QAction(tr("Cut"), this);
     pasteA = new QAction(tr("Paste"), this);
}
